package com.woniuxy.stall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.woniuxy.core.exception.certifiedException.CertifiedException;
import com.woniuxy.core.exception.certifiedException.CertifiedExceptionCode;
import com.woniuxy.core.exception.stallException.StallException;
import com.woniuxy.core.exception.stallException.StallExceptionCode;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.core.staticCode.StallInfoCode;
import com.woniuxy.renters.model.form.AddCarportForm;
import com.woniuxy.stall.dao.StallCheck;
import com.woniuxy.stall.feign.RentersClient;
import com.woniuxy.stall.mapper.StallCheckMapper;
import com.woniuxy.stall.model.Param.PassStallCheckParam;
import com.woniuxy.stall.model.Param.loadAllStallCheckParam;
import com.woniuxy.stall.model.dto.StallCheckDto;
import com.woniuxy.stall.model.form.AddStallForm;
import com.woniuxy.stall.service.StallCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.stall.service.StallInfoService;
import com.woniuxy.stall.service.param.AddStallParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@Service
public class StallCheckServiceImpl extends ServiceImpl<StallCheckMapper, StallCheck> implements StallCheckService {

    @Resource
    private StallCheckMapper stallCheckMapper;
    @Resource
    private RentersClient rentersClient;

    @Resource
    private StallInfoService stallInfoService;


    @Override
    public void passStallCheck(PassStallCheckParam param) {
        //是否存在该车位申请
        StallCheck stallCheck = stallCheckMapper.selectById(param.getStallCheckId());
        if(stallCheck==null){
            throw new StallException(StallExceptionCode.STALL_CHECK_NO_HAVE);
        }
        //该申请是否已经通过
        if(stallCheck.getStallCheckState()==StallInfoCode.STALL_CHECK_SUCCESS.getCode()){
            throw new CertifiedException(CertifiedExceptionCode.DO_NOT_REPEAT_OPERATION);
        }
        //将状态设置为通过
        stallCheck.setStallCheckState(StallInfoCode.STALL_CHECK_SUCCESS.getCode());
        stallCheckMapper.updateById(stallCheck);
        //新增车位
        AddStallForm addStallForm = new AddStallForm();
        addStallForm.setStallInfoNum(stallCheck.getStallInfoNum());
        addStallForm.setStallTpye(param.getStallType());
        addStallForm.setStallInfoAffiliation(StallInfoCode.STALL_OF_RENTER.getCode());
        addStallForm.setCertifiedInfoId(stallCheck.getCertifiedInfoId());
        List<AddStallForm> list = new ArrayList<>();
        list.add(addStallForm);
        List<AddStallParam> params=new ArrayList<>();
        for (AddStallForm form : list) {
            AddStallParam param1 = BeanUtil.toBean(form, AddStallParam.class);
            param1.setCertifiedInfoId(form.getCertifiedInfoId());
            params.add(param1);
        }
        List<Integer> integers = stallInfoService.certifiedAddStall(params);
        //将通过的车位id加在申请表
        stallCheck.setStallInfoId(integers.get(0));
        stallCheckMapper.updateById(stallCheck);
        //将通过的车位id加在出租客详情表中
        AddCarportForm form = new AddCarportForm();
        form.setRentersInfoId(param.getRentersInfoId());
        form.setStallInfoId(integers.get(0).toString());
        System.out.println("form:===================>"+form);
        rentersClient.addCarport(form);


    }

    @Override
    public PageInfo<StallCheckDto> loadAllStallCheck(loadAllStallCheckParam param) {
        Page<StallCheck> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
        QueryWrapper<StallCheck> wrapper = new QueryWrapper<>();
        wrapper.eq("certified_info_id",param.getCertifiedInfoId());
        wrapper.eq("stall_check_state",StallInfoCode.STALL_WAITING_CHECK.getCode());
        wrapper.orderByDesc("stall_check_id");
        stallCheckMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }

    @Override
    public void rejectStallCheck(Integer stallCheckId) {
        //是否存在该车位申请
        StallCheck stallCheck = stallCheckMapper.selectById(stallCheckId);
        if(stallCheck==null){
            throw new StallException(StallExceptionCode.STALL_CHECK_NO_HAVE);
        }
        //更改为驳回状态
        stallCheck.setStallCheckState(StallInfoCode.STALL_CHECK_FAIL.getCode());
        stallCheckMapper.updateById(stallCheck);
    }
}
